package com.elinkway.infinitemovies.tips;

import android.graphics.drawable.AnimationDrawable;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.elinkway.infinitemovies.utils.DensityUtil;
import com.elinkway.infinitemovies.view.BaseRecyclerFragment;
import com.le123.ysdq.R;
import com.recyclerlayout.refresh.RecyclerRefreshLayout;

public class DefaultTipsHelper implements TipsHelper {

    protected final BaseRecyclerFragment mFragment;
    protected final RecyclerView mRecyclerView;
    protected final RecyclerRefreshLayout mRefreshLayout;
    protected final ImageView mLoadingView;

    public DefaultTipsHelper(BaseRecyclerFragment fragment) {
        mFragment = fragment;
        mRecyclerView = fragment.getRecyclerView();
        mRefreshLayout = fragment.getRecyclerRefreshLayout();

        mLoadingView = new ImageView(fragment.getActivity());
        mLoadingView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        mLoadingView.setImageResource(R.drawable.spinner);
        mLoadingView.setPadding(0, (int) DensityUtil.dip2px(mFragment.getActivity(), 10),
                0, (int) DensityUtil.dip2px(mFragment.getActivity(), 10));
        mLoadingView.setLayoutParams(new RecyclerView.LayoutParams(
                RecyclerRefreshLayout.LayoutParams.MATCH_PARENT,
                (int) DensityUtil.dip2px(fragment.getActivity(), 40)));
    }

    @Override
    public void showEmpty() {
        hideLoading();
        TipsUtils.showTips(mRecyclerView, TipsType.EMPTY);
    }

    @Override
    public void hideEmpty() {
        TipsUtils.hideTips(mRecyclerView, TipsType.EMPTY);
    }

    @Override
    public void showLoading(boolean firstPage) {
        hideEmpty();
        hideError();
        if (firstPage) {
            View tipsView = TipsUtils.showTips(mRecyclerView, TipsType.LOADING);
            AnimationDrawable drawable = (AnimationDrawable) ((ImageView) tipsView).getDrawable();
            drawable.start();
            return;
        }

//        maybe you like this mode
//        mRefreshLayout.post(new Runnable() {
//          @Override
//          public void run() {
//            mRefreshLayout.setRefreshing(true);
//          }
//        });
    }

    @Override
    public void hideLoading() {
        TipsUtils.hideTips(mRecyclerView, TipsType.LOADING);
    }

    @Override
    public void showError(boolean firstPage, Throwable error) {
    }

    @Override
    public void hideError() {
        TipsUtils.hideTips(mRecyclerView, TipsType.LOADING_FAILED);
    }

    @Override
    public void showHasMore() {
    }

    @Override
    public void hideHasMore() {
    }
}
